module IfItWerent4AR
  module Associations
    def add_associations
      IfItWerent4AR::Base.descendants.each do |descendant|
        if descendant.has_many
          descendant.has_many.each do |hm|
            descendant.class_eval do
              define_method(hm.to_s.underscore.pluralize.to_sym) do
                hm.where("#{descendant.to_s.underscore}_id": self.id)
              end
            end
          end
        end

        if descendant.belongs_to
          descendant.belongs_to.each do |bt|
            descendant.class_eval do
              define_method(bt.to_s.underscore.to_sym) do
                bt.find(self.public_send("#{bt.to_s.underscore}_id"))
              end
            end
          end
        end
      end
    end
  end
end
